<?php

namespace App\Http\Controllers;

use App\Comment;
use App\Discuss;
use App\Http\Requests;
use App\Parsedown;
use App\Study;
use App\Studyfav;
use App\Task;
use App\Judgetask;
use App\User;
use App\Utils;
use Auth;
use Cache;
use Cookie;
use DB;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Input;
use Log;
use Mail;
use Redirect;
use Reponse;
use View;
use Crypt;

class contestController extends Controller
{
    public function __construct()
    {
    }
	function cmp($a,$b)
	{
		return $a['s']>$b['s'];
	}
	public function rank(Request $request)
	{
		if(Auth::check())
		{

			$judges=DB::select('select status,judge_info,userid from judgetasks where taskid=146 and userid='.Auth::user()->id);
		
		}
			$page = 1;
			$input = $request->all();
			if (isset($input['page']))
				$page = $request->all()['page'];
			$judges=DB::select('select status,judge_info,userid from judgetasks where taskid=146');
			$arr=array();
			foreach($judges as $judge)
			{
				if($judge->status!=1)continue;
				if($judge->userid==19)continue;
				$judgeInfo=json_decode($judge->judge_info,true);
				if($judgeInfo['result']!="Accepted")continue;
				if(!isset($arr[$judge->userid]['user']))
				{
					$arr[$judge->userid]['user']=User::find($judge->userid);
					$arr[$judge->userid]['s']=0;
				}
				if($arr[$judge->userid]['s']==0)
				$arr[$judge->userid]['s']=$judgeInfo['total_cost'];
				else
				$arr[$judge->userid]['s']=min($arr[$judge->userid]['s'],$judgeInfo['total_cost']);
			}
			usort($arr,'self::cmp');
			$judges=$arr;
			$type="ranks";
			return View::make('contest.rank')
				->with(compact('judges'))
				->with(compact('type'));
		}


	public function award(Request $request)
	{
		$arr=array(
			"Cgo8PFTUWAaAbD7PAACAXTzCmiw113.jpg",
			"CgqKkVdCbJuAMNEuAAA74uyA4kY648.jpg",
			"146967681956343.jpg",
			"Cgo8PFTUWBqAYNcoAACVuXBudcE787.png",
			"CgqKkVdefqCAeTtAAABaDqGTOvY825.png",
			"Cgo8PFTUXeeAEH9nAACa9TE9QOc377.jpg",
			"Cgo8PFTUXNyAf-oyAAB84XZJshI598.png",
			"Cgo8PFUnbyyAOmwZAABKPABIzv8461.png",
			"CgqLKVYt3X6AM3jeAAA7ivNTiPg788.jpg",
			"Cgo8PFWcoTKARPNOAABLnbTivc8708.png",
			"CgYXBlTUV_uAY85GAAAVtFKXIv8322.jpg",
			"Cgp3O1bNGd-AONn6AAAkBE4rh7k087.png",
			"CgYXBlTUXIWAI6-_AACELhVYx_0151.jpg",
			"Cgp3O1bOkfGAY16IAADMaKzi7qM733.png",
			"CgYXBlTUXKOAI9eHAAB-EazTn0Y320.png",
			"CgYXBlUydMqACbbOAAB3gNFKizc967.png");
		$type="award";
		return View::make('contest.award')
			->with(compact('arr'))
			->with(compact('type'))
			;
	}


	public function discuss(Request $request)
	{
		if(Auth::check())
		{

			$judges=DB::select('select status,judge_info,userid from judgetasks where taskid=146 and userid='.Auth::user()->id);
			$cnt=count($judges);
			if($cnt>2)
			{
				echo "食年: 各位赛码会2的参赛选手,非常的抱歉。2016.11.26号，我查阅了这26天所有选手的方案，并没有一个有实质性的进展，大多数是基于数据的hack。（我之前的预测是有10个选手能突破180000分）。因此不得不承认这是一场非常非常失败的比赛，并且主要的原因是我选题的失败。top50的用户的方案甚至没有实质性差距。基于以上原因，我决定停止这次的比赛，对于出于学习目的的参赛者，我会在近日公布稍微好的标程。 对于出于拿奖品为目的的参赛者，非常的抱歉浪费了你宝贵的时间。奖品只能顺延至下期。";
				die;
			}
		}
		$questions=Discuss::where('id',">",293)->get();
        foreach ($questions as $q) {
            $q->user = User::find($q->userid);
            $comment = Comment::whereTypeAndTaskidAndDelete(1, $q->id, 0)->orderBy("created_at", "desc")->take(1)->get();
            if (count($comment) == 0) continue;
            $comment[0]->user = User::find($comment[0]->userid);
            $q->firstc = $comment[0];

        }
		$type="discuss";
		return View::make('contest.discuss')
			->with(compact('questions'))
			->with(compact('type'))
			;
	}

	public function detail(Request $request)
	{
		if(Auth::check())
		{

			$judges=DB::select('select status,judge_info,userid from judgetasks where taskid=146 and userid='.Auth::user()->id);
			$cnt=count($judges);
			if($cnt>2)
			{
				echo "食年: 各位赛码会2的参赛选手,非常的抱歉。2016.11.26号，我查阅了这26天所有选手的方案，并没有一个有实质性的进展，大多数是基于数据的hack。（我之前的预测是有10个选手能突破180000分）。因此不得不承认这是一场非常非常失败的比赛，并且主要的原因是我选题的失败。top50的用户的方案甚至没有实质性差距。基于以上原因，我决定停止这次的比赛，对于出于学习目的的参赛者，我会在近日公布稍微好的标程。 对于出于拿奖品为目的的参赛者，非常的抱歉浪费了你宝贵的时间。奖品只能顺延至下期。";
				die;
			}
		}
		$task=Task::find(146);
		$type="detail";
		return View::make('contest.detail')
			->with(compact('task'))
			->with(compact('type'))
			;
	}
}
